window.onload = function () {
    _init_area();
}
    //避免变量冲突和污染
    ;
(function () {

    $(function () {
        $.idcode.setCode(); //加载生成验证码方法

        //给表单绑定事件
        $("form:eq(0)").validate({
            //规则
            rules: {
                uname: {
                    required: true, //必填
                    rangelength: [3, 50],
                    // checkName: true
                },
                upwd: {
                    required: true, //必填
                    rangelength: [6, 20],
                },
                upwd2: {
                    equalTo: "[name='upwd']"
                },
                email: {
                    email: true,
                }
            },
            //提示
            messages: {
                uname: {
                    required: '用户名必填',
                    rangelength: "用户名必须是{0}-{1}",
                },
                upwd: {
                    required: '密码必填',
                    rangelength: "密码必须是{0}-{1}"
                },
                upwd2: {
                    equalTo: "2次密码不一致"
                },
                uemail: {
                    email: "邮箱不合法"
                }
            },
            //提交
            submitHandler: function () {

                var IsBy = $.idcode.validateCode();

                if (!IsBy) {

                    layer.msg('验证码错误!');
                    return false;
                } else {

                    //校验都通过,注册到cookie
                    let u_address = `${$("#s_province").val()}_${$("#s_city").val()}_${$("#s_county").val()}`;
                    var formdata = new FormData();
                    formdata.append("u_name", $('[name="uname"]').val());
                    formdata.append("u_pwd", $('[name="upwd"]').val());
                    formdata.append("u_address", u_address);
                    formdata.append("u_email", $('[name="uemail"]').val());
     
                    // 存储到数据库
                    $.ajax({
                        url: "./api/reg",
                        data: formdata,
                        type: "post",
                        processData: false,
                        contentType: false
                    }).done(function (res) {
                        //注册成功,是否理解去登录 状态为1
                        if (res.status == 1) {
                            layer.confirm(`${res.msg},是否立即去登录?`, {
                                skin: 'layui-layer-molv',
                                type: 1,
                                btn: ["yes", "noyes"]
                            }, (index) => {
                                layer.close(index);
                                location = "./login.html";
                            }, () => { });
                        }else{
                            layer.msg(res.msg);
                        }
                    })
                }

                return false;

            }
        })
    });
})();



